﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO.Ports;
using System.Text.RegularExpressions;
using System.Threading;
using System.Reflection; // 引用这个才能使用Missing字段
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Xml;
using System.Web;
using System.Runtime.InteropServices;
using CCWin;
using System.Windows.Forms;

using System.ComponentModel;

namespace weiqijiance.安检仪表
{
    /// <summary>
    /// 地沟类（用于多地沟检测）
    /// </summary>
    public  class flowAP
    {
        public diaodu objdd;// = new diaodu();
        public 控制流程.flowcrtl flowbass ;//= new 控制流程.flowcrtl();
        public string stiaoma="";
        public bool APbegin = false;
        public bool APend = false;
        Thread t1;
        public string ssx = "";
        public string sxx = "";
        public string revert = "";
        public bool bdpjxylx ;
        public string start()
        {
            if (flowbass.izhuangtai ==1)
            {
                MessageBox.Show("流水号"+flowbass.sDetectID+"正在检测，该工位状态忙碌中");
                return "";
            }
            flowbass.objcarclass.jcxdh = flowbass.ddgxh;
            flowbass.objcarclass.tongdao1 = flowbass.iaptongdao.ToString();
            flowbass.objcarclass.tongdao2 = flowbass.idpjxytongdao.ToString();
            flowbass.izhuangtai = 1;
            t1 = new Thread(thread1);
            t1.Start();
            return "";
        }
        public void readshezhitime()
        {


            string path = "";
            path = System.Windows.Forms.Application.StartupPath + path + "\\安综检仪表配置.ini";
            string path1 = string.Format(@"{0}", path);
            iniclass objini = new iniclass();
            objini.Ini(path1);

            showtime = objini.ReadValue("设置", "地沟显示检测时间").Trim();
            bdpjxylx = objini.ReadValue("地沟", "增加底盘间隙仪录像").Trim().Contains("是") ? true : false;


        }
        void thread1()
        {
            StratTest();
            flowbass.LedWrite(flowbass.sgongwei, "", 1);
            ssx=flowbass.sgongwei;
            sxx="";
            objdd.showdp(this);
            //xianshineirong = flowbass.sgongwei;
            //xianshi();
        }
        public string showtime;
        public void StratTest()
        {
            readshezhitime();
            string xianshineirong = "";
            APbegin = false;
            APend = false;
            if (!flowbass.bJCDG)
            {
                xianshineirong = string.Format(@"底盘免检");
                flowbass.LedWrite(flowbass.sPlate, xianshineirong, 1);
                ssx = flowbass.sPlate;
                sxx = xianshineirong;
                objdd.showdp(this);
                return;
            }
            xianshineirong = string.Format(@"底盘检查");
            stiaoma = "";
            flowbass.LedWrite(flowbass.sPlate, xianshineirong, 1);
            ssx = flowbass.sPlate;
            sxx = xianshineirong;
            objdd.showdp(this);
            while (true)
            {
                Thread.Sleep(10);
                
               if (stiaoma.Contains("9995") || APbegin)
                {
                    flowbass.UpdateLoginRecord(DateTime.Now.ToString(), "底盘检验开始", flowbass.sDetectID);

                    xianshineirong = string.Format(@"底盘检查开始");


                    flowbass.LedWrite(flowbass.sPlate, xianshineirong, 1);

                    // 红外定位
                    if (flowbass.bnj || flowbass.bsj)
                    {                  
                        revert = flowbass.objajlwbass.SendJYXMKSSJ("C1", "开始", flowbass.gwjysbbh, flowbass.iaptongdao.ToString(), flowbass.objcarclass);
                        if (!(revert.Length > 0 && revert[0].ToString() == "1"))
                        {
                            flowbass.LedWrite("联网失败", revert, 1); Thread.Sleep(2000);
                        }
                    }

                    break;
                }
                if (flowbass.IsCancle())
                {
                    return;
                }
            }


            ssx = flowbass.sPlate;
            sxx = xianshineirong;
            objdd.showdp(this);
            stiaoma = "";


            //
            string sbuhege = "";
            bool Dcconlusion = false;
            //测量数据
            DateTime dstart = DateTime.Now;
            var  datatime = 0;
            while (true)
            {
                if (showtime.Contains("是") && flowbass.DateDiff(DateTime.Now, dstart) > 1000)
                {
                    dstart = DateTime.Now;
                    datatime++;
                    xianshineirong = string.Format(@"底盘已检{0}秒", datatime);
                    flowbass.LedWrite(flowbass.sPlate, xianshineirong, 1);
                }
                Thread.Sleep(10);
                
                if (flowbass.IsCancle())
                {
                    return;
                }
                if (stiaoma.Contains("9995"))
                {
                    stiaoma = "";
                }
                if (stiaoma.Contains("9999") || APend)
                {
                    if (flowbass.bnj || flowbass.bsj)
                    {
                        flowbass.objajlwbass.sendpic(flowbass.iaptongdao.ToString(), "0323", flowbass.objcarclass);//图片抓拍
                    }
                    xianshineirong = string.Format(@"底盘检查完成");

                    flowbass.LedWrite(flowbass.sPlate, xianshineirong, 1);
                    ssx = flowbass.sPlate;
                    sxx = xianshineirong;
                    objdd.showdp(this);
                    stiaoma = "";
                    flowbass.UpdateLoginRecord(DateTime.Now.ToString(), "底盘检验结束", flowbass.sDetectID);

                    break;
                }
                else
                {

                    if (flowbass.sjcbz.Contains("38900"))
                    {
                        sbuhege = showbhg38900(sbuhege);
                    }
                    else
                    {
                        sbuhege = showbhg(sbuhege);
                    }
                    //if (stiaoma != "")
                    //{
                    //    sbuhege = sbuhege + "," + stiaoma;
                    //    stiaoma = "";//
                    //}
                }

                Thread.Sleep(300);
            }
            if ((sbuhege + "").Length < 1)
            {
                Dcconlusion = true;
            }
            if (!Dcconlusion)
            {
                flowbass.getbuhge(flowbass.sDetectID, "C1");
                //flowbass.updatejl(JL, flowbass.sDetectID);
            }
            else
            {
                flowbass.sethege(flowbass.sDetectID, "C1");
                ////flowbass.updatejl(JL, flowbass.sDetectID);
            }
            //检测结束
           

            flowbass.AddData(flowbass.sDetectID, "DET_DCJL", "Detects_SJA", Dcconlusion ? 1 : 2);

            flowbass.AddData_Check(flowbass.sDetectID, "DET_DC", "Detects_SJA", sbuhege);

            //检测结束

            //安检数据检测数据上传
            model.安检联网.modelC1 objmodellw = new model.安检联网.modelC1();

            if (flowbass.sjcbz.Contains("38900"))
            {
                objmodellw.rzxxbj = sbuhege.Contains("47") ? "2" : "1";//c测试
                objmodellw.rcdxbj = sbuhege.Contains("48") ? "2" : "1";
                objmodellw.rxsxbj = sbuhege.Contains("49") ? "2" : "1";
                objmodellw.rzdxbj = sbuhege.Contains("50") ? "2" : "1";
                objmodellw.rqtbj = sbuhege.Contains("51") ? "2" : "1";
            }
            else
            {
                objmodellw.rzxxbj = sbuhege.Contains("45") ? "2" : "1";//c测试
                objmodellw.rcdxbj = sbuhege.Contains("46") ? "2" : "1";
                objmodellw.rxsxbj = sbuhege.Contains("47") ? "2" : "1";
                objmodellw.rzdxbj = sbuhege.Contains("48") ? "2" : "1";
                objmodellw.rqtbj = sbuhege.Contains("49") ? "2" : "1";
            }
            //objmodellw.rzxxbj = sbuhege.Contains("45") ? "2" : "1";//c测试
            //objmodellw.rcdxbj = sbuhege.Contains("46") ? "2" : "1";
            //objmodellw.rxsxbj = sbuhege.Contains("47") ? "2" : "1";
            //objmodellw.rzdxbj = sbuhege.Contains("48") ? "2" : "1";
            //objmodellw.rqtbj = sbuhege.Contains("49") ? "2" : "1";

            if (flowbass.sPclass.Contains("挂") || flowbass.sCllx.Substring(0, 1).Contains("B") || flowbass.sCllx.Substring(0, 1).Contains("G"))
            {
                objmodellw.rzxxbj = "0";
                objmodellw.rcdxbj = "0";
            }
            objmodellw.ycy = flowbass.sYCY;
            objmodellw.ycysfzh = flowbass.getycysfz(flowbass.sYCY);
            objmodellw.dpjcjyy=flowbass.sDPCZY;
            objmodellw.dpjyysfzh= flowbass.getycysfz(flowbass.sDPCZY);
            //flowbass.AddData_Check(flowbass.sDetectID, "DET_DPCZY", "Detects_SJA", flowbass.sDPCZY);//底盘检验员
            //数据上传
            if (flowbass.bnj || flowbass.bsj)
            {
                revert = flowbass.objajlwbass.write(flowbass.objcarclass, objmodellw);
                if (!(revert.Length > 0 && revert[0].ToString() == "1"))
                {
                    flowbass.LedWrite("联网失败", revert, 1); Thread.Sleep(2000);
                }

                revert = flowbass.objajlwbass.SendJYXMKSSJ("C1", "结束", flowbass.gwjysbbh, flowbass.iaptongdao.ToString(), flowbass.objcarclass);
                if (!(revert.Length > 0 && revert[0].ToString() == "1"))
                {
                    flowbass.LedWrite("联网失败", revert, 1); Thread.Sleep(2000);
                }
            }
            flowbass.getProcessfinish(flowbass.sDetectID, "C1");
/////////////////////////发送总结束判定
            System.Threading.Thread.Sleep(1000);
            if (flowbass.LastProcessnew(int.Parse(flowbass.sDetectID)) && !flowbass.ifinish)
            {
                flowbass.LedWrite("正在发送", "联网总结束", 1);
                string slianwangfanhui = flowbass.objajlwbass.sendAJend(flowbass.jylsh, false, flowbass.bzj);
                if (!(slianwangfanhui.Length > 0 && slianwangfanhui[0].ToString() == "1"))
                {
                    flowbass.LedWrite("联网失败", slianwangfanhui, 1); 
                }
                else
                {
                    flowbass.LedWrite("发送完成", "", 1);
                    flowbass.UpdateLoginRecord(DateTime.Now.ToString(), "检测线完成", flowbass.sDetectID);
                }
                Thread.Sleep(2000);
            }
            if (flowbass.LastProcessnewzj(int.Parse(flowbass.sDetectID))&&(flowbass.bzj||flowbass.bsj))
            {
                xianshineirong = "检测线完成";
                flowbass.UpdateLoginRecord(DateTime.Now.ToString(), "检测线完成", flowbass.sDetectID);
                flowbass.LedWrite(flowbass.sPlate, xianshineirong, 1);
                flowbass.sethegezj(flowbass.sDetectID);
                Thread.Sleep(2000);
            }
            flowbass.Updatejcxhao(flowbass.objcarclass.jcxdh, flowbass.sDetectID);
            flowbass.izhuangtai = 0;
        }
        string showbhg(string sbuhege)
        {
            string xianshineirong="";
            if (stiaoma.Contains("0045"))//不合格项目
            {
                sbuhege = sbuhege + "0045,";
                xianshineirong = string.Format(@"不合格项：{0}", stiaoma);

                flowbass.LedWrite(flowbass.sPlate, xianshineirong, 1);
                ssx = flowbass.sPlate;
                sxx = xianshineirong;
                objdd.showdp(this);
                stiaoma = "";
            }
            if (stiaoma.Contains("0046"))
            {
                sbuhege = sbuhege + "0046,";
                xianshineirong = string.Format(@"不合格项：{0}", stiaoma);

                flowbass.LedWrite(flowbass.sPlate, xianshineirong, 1);
                ssx = flowbass.sPlate;
                sxx = xianshineirong;
                objdd.showdp(this);
                stiaoma = "";
            }
            if (stiaoma.Contains("0047"))
            {
                sbuhege = sbuhege + "0047,";
                xianshineirong = string.Format(@"不合格项：{0}", stiaoma);

                flowbass.LedWrite(flowbass.sPlate, xianshineirong, 1);
                ssx = flowbass.sPlate;
                sxx = xianshineirong;
                objdd.showdp(this);
                stiaoma = "";
            }
            if (stiaoma.Contains("0048"))
            {
                sbuhege = sbuhege + "0048,";
                xianshineirong = string.Format(@"不合格项：{0}", stiaoma);

                flowbass.LedWrite(flowbass.sPlate, xianshineirong, 1);
                ssx = flowbass.sPlate;
                sxx = xianshineirong;
                objdd.showdp(this);
                stiaoma = "";
            }
            if (stiaoma.Contains("0049"))
            {
                sbuhege = sbuhege + "0049,";
                xianshineirong = string.Format(@"不合格项：{0}", stiaoma);

                flowbass.LedWrite(flowbass.sPlate, xianshineirong, 1);
                ssx = flowbass.sPlate;
                sxx = xianshineirong;
                objdd.showdp(this);
                stiaoma = "";
            }
            return sbuhege;
        }
        string showbhg38900(string sbuhege)
        {
            string xianshineirong = "";
            if (stiaoma.Contains("0047"))//不合格项目
            {
                sbuhege = sbuhege + "0047,";
                xianshineirong = string.Format(@"不合格项：{0}", stiaoma);

                flowbass.LedWrite(flowbass.sPlate, xianshineirong, 1);
                ssx = flowbass.sPlate;
                sxx = xianshineirong;
                objdd.showdp(this);
                stiaoma = "";
            }
            if (stiaoma.Contains("0048"))
            {
                sbuhege = sbuhege + "0048,";
                xianshineirong = string.Format(@"不合格项：{0}", stiaoma);

                flowbass.LedWrite(flowbass.sPlate, xianshineirong, 1);
                ssx = flowbass.sPlate;
                sxx = xianshineirong;
                objdd.showdp(this);
                stiaoma = "";
            }
            if (stiaoma.Contains("0049"))
            {
                sbuhege = sbuhege + "0049,";
                xianshineirong = string.Format(@"不合格项：{0}", stiaoma);

                flowbass.LedWrite(flowbass.sPlate, xianshineirong, 1);
                ssx = flowbass.sPlate;
                sxx = xianshineirong;
                objdd.showdp(this);
                stiaoma = "";
            }
            if (stiaoma.Contains("0050"))
            {
                sbuhege = sbuhege + "0050,";
                xianshineirong = string.Format(@"不合格项：{0}", stiaoma);

                flowbass.LedWrite(flowbass.sPlate, xianshineirong, 1);
                ssx = flowbass.sPlate;
                sxx = xianshineirong;
                objdd.showdp(this);
                stiaoma = "";
            }
            if (stiaoma.Contains("0051"))
            {
                sbuhege = sbuhege + "0051,";
                xianshineirong = string.Format(@"不合格项：{0}", stiaoma);

                flowbass.LedWrite(flowbass.sPlate, xianshineirong, 1);
                ssx = flowbass.sPlate;
                sxx = xianshineirong;
                objdd.showdp(this);
                stiaoma = "";
            }
            return sbuhege;
        }
        private SerialPort comAP = new SerialPort();//地沟串口

        public bool openCOM(string sSet1, SerialPort commex)//打开串口
        {

            string[] szbzlCOM = new string[5];
            if (sSet1.Length > 0)
                szbzlCOM = sSet1.Split(':');
            //打开串口
            commex.PortName = szbzlCOM[0];
            commex.BaudRate = Convert.ToInt32(szbzlCOM[1]);

            commex.NewLine = "/r/n";
            commex.RtsEnable = true;//根据实际情况吧。 
            commex.ReadTimeout = 300;//读取数据延时，避免读取不全数据

            if (szbzlCOM[3] == "0")
                commex.Parity = Parity.None;
            else
                commex.Parity = Parity.Even;
            // ////添加事件注册  
            //  comm.DataReceived += comm_Data;
            try
            {
                commex.Open();
            }
            catch (Exception )
            {
                //lzj20200706    MessageBox.Show(string.Format("{0}\n请检查电脑的串口故障", ex.Message));
                return false;
            }
            return true;
        }
        public void initcom(string sSetCom)
        {
            //comAP = com;
            openCOM(sSetCom, comAP);
            comAP.ReadTimeout = 500;
            //comAP.ReceivedBytesThreshold = 10;//达到50字节以后才开始触发串口
            comAP.DataReceived += comAP_DataReceived;
        }

        void comAP_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            if (comAP.IsOpen)
            {
                int n = comAP.BytesToRead;//
                byte[] buf1 = new byte[n];//声明一个临时数组存储当前来的串口数据  
                byte[] buf = new byte[n];
                comAP.Read(buf1, 0, n);

                string scom = "";
                scom = System.Text.Encoding.Default.GetString(buf1);
                stiaoma = scom;
            }

        }
      
    }
}
